#include "led.h"
#include <Arduino.h>
#include "MyWifi.h"


extern WIFI_STATE wifiState;
char ledStat = 0;

void led_on(void)
{
    digitalWrite(LED_WIFI_STAT_PIN,1);
    ledStat = 1;
}

void led_off(void)
{
    digitalWrite(LED_WIFI_STAT_PIN,0);
    ledStat = 0;
}

void led_task(void *pvParameters)
{
    int timeStamp = 0;
    while (1)
    {   
        switch(wifiState)
        {
            case WIFI_STAT_CONNECT:
                led_on();
                break;
            case WIFI_STAT_INIT:
                if(timeStamp % 150 == 0)
                {
                    if(ledStat == 1)
                    {
                        led_off();
                    }
                    else
                    {
                        led_on();
                    }
                }
                break;
            case WIFI_STAT_DISCONNECT:
                led_off();
                break;
            case WIFI_STAT_SMART:
            case WIFI_STAT_SMART_FAIL:
            case WIFI_STAT_SMART_SUCCESS:
                if(timeStamp % 50 == 0)
                {
                    if(ledStat == 1)
                    {
                        led_off();
                    }
                    else
                    {
                        led_on();
                    }
                }
                break;
            default:
                break;
        }
        timeStamp ++;
        vTaskDelay(10/portTICK_PERIOD_MS);
    }
}

void led_init(void)
{
    pinMode(LED_WIFI_STAT_PIN, OUTPUT);
    led_on();
}

